package 死锁;

public class Test {
    public static void main(String[] args){
        Object lock1 = new Object();
        Object lock2 = new Object();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized(lock1){
                    System.out.println("线程t1拿到lock1");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程t1尝试在拿到lock1的情况下获取lock2...");
                    synchronized(lock2){
                        System.out.println("线程t1拿到lock2");
                    }
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized(lock1){
                    System.out.println("线程t2拿到lock1");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程t2尝试在拿到lock1的情况下获取lock2...");
                    synchronized(lock2) {
                        System.out.println("线程t2拿到lock2");
                    }
                }
            }
        });
        t1.start();
        t2.start();
    }
}
